import { v4 as uuid } from 'uuid'
import { body, buttons } from '@/constants/keyword'
import { Message } from 'element-ui'


/** 取消关键词 */
export function removeKeyword(editor, node) {
  editor.dom.removeAllAttribs(node)
  editor.selection.select(node)
  editor.formatter.remove('keyword')
  editor.fire('keywordRemove')
}


export default function (editor) {
  // 关键词
  editor.ui.registry.addToggleMenuItem('contextmenu_keyword', {
    text: '关键词',
    onAction() {
      editor.windowManager.open({
        title: '关键词',
        body,
        buttons,
        initialData: {
          name: ''
        },
        async onSubmit(api) {
          const { name } = api.getData()
          if(!name || !name.trim()) {
            Message.warning('请输入内容')
            return
          }
          editor.formatter.apply('keyword', { title: name, dataId: uuid() })
          editor.fire('keywordAdd')
          api.close();
        }
      })
    }
  })
  // 取消关键词
  editor.ui.registry.addToggleMenuItem('contextmenu_keyword_cancle', {
    text: '取消关键词',
    onAction() {
      removeKeyword(editor, editor.selection.getNode())
    }
  })

  // console.debug('---右键菜单: 关键词---')
}
